<?php

declare(strict_types=1);
namespace App\Common\transactions;


use Hyperf\DbConnection\Db;
use Psr\Log\LoggerInterface;
use Hyperf\Di\Annotation\Inject;

/**
 *  筛子策略关联统计
 */
class SieveBehaviorLog
{



    #[Inject]
    protected LoggerInterface $logger;


    private string $first_pay_num = 'first_pay_num'; //首充惩罚分
    private string $water_num = 'water_num'; //流水惩罚分
    private string $withdraw_num = 'withdraw_num'; //退款惩罚分
    private string $ip_user = 'ip_user'; //ip关联用户
    private string $phone_user = 'phone_user'; //退款手机关联用户
    private string $email_user = 'email_user'; //退款邮箱关联用户
    private string $bank_code_user = 'bank_code_user'; //银行卡关联用户


    /**
     * 修改或者插入 筛子策略关联统计
     * @param $uid int|string 用户UID
     * @param $field array  ['first_pay_num','water_num','withdraw_num','ip_user','phone_user','email_user','bank_code_user']//	首充惩罚分,流水惩罚分,退款惩罚分,	ip关联用户,退款手机关联用户,退款邮箱关联用户,银行卡关联用户
     * @param  $data array [],这里要上面的字段对应
     */
    public function setSieveBehaviorLog(int|string $uid, array $field = [], array $data = []){
        if(in_array('uid',$data))
            Db::table('sieve_behavior_log')->updateOrInsert(
                ['uid' => $uid],
                $data
            );
    }




    private function getData($uid,array $field = [], array $data = []){
        $installOrUpdate = [];
        foreach ($field as $key => $val){
            $list = match($val){
                $this->first_pay_num => $this->getFirstPayNum($data[$key]),
                $this->water_num => $this->getWaterNum($uid),
                $this->withdraw_num => 1,
                $this->ip_user => 1,
                $this->phone_user => 1,
                $this->email_user => 1,
                $this->bank_code_user => 1,
            };
            if($list)$installOrUpdate[]  = $list;
        }

    }


    private function getFirstPayNum($pay_money):int{
        $point = Db::table('sieve_behavior_config')->where([['type','=',1],['num','<',$pay_money]])->orderBy('num','desc')->value('point');
        if($point)return $point;
        return 0;
    }


    private function getWaterNum($uid):int{
        $userinfo = Db::table('userinfo')->selectRaw('total_pay_score,total_give_score,total_water_score')->where('uid',$uid)->first();
        if(!$userinfo)return 0;
        $total_water_score = bcadd($userinfo['total_pay_score'],$userinfo['total_give_score'],0) <= 0 ? bcdiv($userinfo['total_water_score'],'10000',2) : bcdiv($userinfo['total_water_score'],bcadd($userinfo['total_pay_score'],$userinfo['total_give_score'],0),2); //打码量比例
        $point = Db::table('sieve_behavior_config')->where([['type','=',1],['num','<',$total_water_score]])->orderBy('num','desc')->value('point');
        if($point)return $point;
        return 0;
    }

    private function getWithdrawNum($uid):int{
        $userinfo = Db::table('userinfo')->selectRaw('total_pay_score,total_give_score,total_exchange')->where('uid',$uid)->first();
        if(!$userinfo)return 0;
        $total_water_score = bcadd($userinfo['total_pay_score'],$userinfo['total_give_score'],0) <= 0 ? bcdiv($userinfo['total_water_score'],'10000',2) : bcdiv($userinfo['total_water_score'],bcadd($userinfo['total_pay_score'],$userinfo['total_give_score'],0),2); //打码量比例
        $point = Db::table('sieve_behavior_config')->where([['type','=',1],['num','<',$total_water_score]])->orderBy('num','desc')->value('point');
        if($point)return $point;
        return 0;
    }
}


